<?php
/**
  * CacheFile.php
  *
  * This is not a freeware, use is subject to license terms
  * @author CLOUD DEV TEAM
  * 
  */
if (!defined('IN_CLOUD')) exit ('Access Denied!');

class CacheFile
{
	/**
	 * 读取缓存
	 * @param string $fileName
	 */
	public static function get( $fileName )
	{
		static $result = array();
		$fileName = ucfirst( $fileName );
		if ( empty( $fileName ) )
		{
			return false;
		}
		if ( $result[$fileName] )
		{
			return $result[$fileName];
		}
		
		$cacheFile = ROOT_DIR . DATA . 'Cache/' . $fileName . '.cache.php';
		if ( file_exists( $cacheFile ) )
		{
			$result[$fileName] = include $cacheFile;
		}
		return $result[$fileName];
	}
	
	/**
	 * 设置缓存
	 * @param string $fileName
	 * @param mix $value
	 */
	public static function set( $fileName , $value )
	{
		if ( empty( $fileName ) )
		{
			return false;
		}
		$fileName = ucfirst( $fileName );
		$cacheFile = ROOT_DIR . DATA . 'Cache/' . $fileName . '.cache.php';
		$data = "<?php\nreturn " . var_export( $value , true ) . ";\n?>";
		$filesize = file_put_contents( $cacheFile , $data , LOCK_EX );
		
		if ( $filesize )
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 设置缓存
	 * @param string $fileName
	 * @param mix $value
	 */
	public static function setConfig( $fileName , $value )
	{
		if ( empty( $fileName ) )
		{
			return false;
		}
		$fileName = ucfirst( $fileName );
		$cacheFile = ROOT_DIR . DATA . 'Config/' . $fileName . '.php';
		$data = "<?php\nreturn " . var_export( $value , true ) . ";\n?>";
		$filesize = file_put_contents( $cacheFile , $data , LOCK_EX );
		
		if ( $filesize )
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 删除缓存
	 * @param string $fileName
	 */
	public static function delete( $fileName )
	{
		if ( empty( $fileName ) )
		{
			return false;
		}
		$fileName = ucfirst( $fileName );
		$cacheFile = ROOT_DIR . DATA . 'Cache/' . $fileName . '.cache.php';
		if ( file_exists( $cacheFile ) )
		{
			return @unlink( $cacheFile ) ? true : false;
		}
		else
		{
			return false;
		}
	}
}